﻿@*------------------------------------------------------------
  Copyright (c) Microsoft Corporation.  All rights reserved.
  Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
------------------------------------------------------------*@

@page "/"
@page "/discoverers"

@using Microsoft.Azure.IIoT.App.Models;
@using Microsoft.Azure.IIoT.App.Services;
@using Microsoft.Azure.IIoT.App.Components.Buttons;
@using Microsoft.Azure.IIoT.App.Components.Drawer;
@using Microsoft.Azure.IIoT.App.Components.Loader;
@using Microsoft.Azure.IIoT.App.Components.ErrorMessage;
@using Microsoft.AspNetCore.Components;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry.Models;
@using Microsoft.Azure.IIoT.App.Common;
@using Microsoft.Azure.IIoT.App.Components.Icons;

@inject NavigationManager NavigationManager
@inject Registry RegistryHelper
@inject IRegistryServiceApi RegistryService
@inject RegistryServiceEvents RegistryServiceEvents
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject UICommon CommonHelper

@inherits IIoTItemsCollection<DiscovererInfo>

<div class="flex">
    <h1>Asset Discovery</h1>
    <a class="adhocmargin" href="javascript: void(0)" @onclick="@(() => OpenDrawer(new DiscovererInfo {
        DiscovererModel = new DiscovererApiModel(),
        isAdHocDiscovery = true
    }))">
        Run ad-hoc discovery
    </a>
    @if (DiscovererData != null)
    {
        if (DiscovererData.IsSearching && DiscovererData.isAdHocDiscovery)
        {
            <div class="spinnermargin loader"></div>
        }
        else
        {
            <div class="spinnermargin"></div>
        }
    }
</div>

<table class="table table-responsive @_tableView">
    <thead>
        <tr>
            <th class="width-large">Discovery Module Id</th>
            <th class="width-medium">Connection Status</th>
            <th class="width-medium">Scanning</th>
            <th class="width-large">Effective Configuration</th>
            <th class="width-small">Version</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var discoverer in Items.Results)
        {
            <tr>
                @if (discoverer.HasApplication == true)
                {
                    <td class="hover-text width-large">
                        <div>
                            <a href='endpoints/@discoverer.DiscovererModel.Id/@RegistryHelper.PathAll/@RegistryHelper.PathAll'>
                                @discoverer.DiscovererModel.Id
                            </a>
                        </div>
                    </td>
                }
                else
                {
                    <td class="hover-text">@discoverer.DiscovererModel.Id</td>
                }
                @{string connectStatus = discoverer.DiscovererModel.Connected == true
                                      ? "Connected"
                                      : "Disconnected";}
                <td class="hover-text width-medium">
                    <ConnectStatus Status="@connectStatus" />
                </td>
                <td class="width-medium">
                    @{bool isDisconnected = discoverer.DiscovererModel.Connected != true;}
                    @{bool isEnabled = discoverer.ScanStatus == true;}
                    @{string scanStatus = isEnabled ? "Turn off" : "Turn on";}

                    <button class="btn btn-primary" disabled=@isDisconnected @onclick="(() => SetScanAsync(discoverer, !isEnabled))">@scanStatus</button>
                    @if (discoverer.IsSearching)
                    {
                        <div class="loader-discovery">
                            <Spinner IsLoading=true></Spinner>
                        </div>
                    }
                </td>
                <td class="width-large">
                    <div class="space-wrap">
                        Mask Address Range: <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                            @(discoverer.EffectiveAddressRangesToScan ?? "Default")<br />
                        </a>
                        Port Range: <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                            @(discoverer.EffectivePortRangesToScan ?? "Default")<br />
                        </a>
                        Max Network Probes: <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                            @(discoverer.EffectiveMaxNetworkProbes ?? "Default")<br />
                        </a>
                        Max Port Probes: <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                            @(discoverer.EffectiveMaxPortProbes ?? "Default")<br />
                        </a>
                        Network Probe Timeout (hh:mm:ss): <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                            @(discoverer.EffectiveNetworkProbeTimeout ?? "Default")<br />
                        </a>
                        Port Probe Timeout (hh:mm:ss): <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                            @(discoverer.EffectivePortProbeTimeout ?? "Default")<br />
                        </a>
                        Idle Time Between Scans (hh:mm:ss): <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                            @(discoverer.EffectiveIdleTimeBetweenScans ?? "Default")<br />
                        </a>
                        Discovery Urls:
                        @if (discoverer.EffectiveDiscoveryUrls.Count == 0)
                        {
                            <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                                (None)
                            </a>
                        }
                        else
                        {
                            @foreach (var url in discoverer.EffectiveDiscoveryUrls)
                            {
                                <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(discoverer))">
                                    @url<br />
                                </a>
                            }
                        }
                    </div>
                </td>
                <td class="hover-text width-small">
                    <div>
                        @discoverer.DiscovererModel.Version
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>

<div class="@_tableEmpty center">No discoverers found.</div>

<div class="results @ScanResult">
    Scan Results:
    <div class="closediv">
        <a aria-label="closePanel" href="javascript: void(0)" @onclick="@(() => CloseScanResultView())" class="oi oi-x closebtn"></a>
    </div>
    <div class="events">
        @EventResult
    </div>
</div>

<ErrorMessage PageError="@Items.Error" Status="@Status"></ErrorMessage>

<Drawer HeaderText="Discovery Scan Config" ObjectData="@DiscovererData" IsOpen="@IsOpen" CloseDrawer="@(() => CloseDrawer())">
    <_DrawerDiscoverer DiscovererData="@DiscovererData" Onclick="((DiscovererData) => ClickHandlerAsync((DiscovererInfo)DiscovererData))"></_DrawerDiscoverer>
</Drawer>

<LoadMore Result=@Items IsLoading=@IsLoading LoadMoreItems=@(async() => await LoadMoreItems()) />

<Spinner IsLoading=@IsLoading></Spinner>